Categories
Quasar

Developing Vue Apps with the Quasar Library — Button Group Options

Spread the love

Quasar is a popular Vue UI library for developing good looking Vue apps.

In this article, we’ll take a look at how to create Vue apps with the Quasar UI library.

Button Group in a Dark Background

We can add a button group in a dark background with the dark prop:

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"
      rel="stylesheet"
      type="text/css"
    />
    <link
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"
      rel="stylesheet"
      type="text/css"
    />
  </head>
  <body class="body--dark">
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>
    <div id="q-app">
      <q-layout
        view="lHh Lpr lFf"
        container
        style="height: 100vh;"
        class="shadow-2 rounded-borders"
      >
        <div class="q-pa-md bg-grey-10 text-white">
          <q-btn-toggle
            v-model="model"
            push
            glossy
            toggle-color="primary"
            :options="options"
          >
          </q-btn-toggle>
        </div>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          model: "",
          options: [
            { label: "Battery low", value: "bat" },
            { label: "Friend request", value: "friend", color: "green" },
            { label: "Picture uploaded", value: "upload", color: "red" }
          ]
        }
      });
    </script>
  </body>
</html>

Button Toggle Content

We can set the options content by populating the slot with the name given by the slot property.

For instance, we can write:

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"
      rel="stylesheet"
      type="text/css"
    />
    <link
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"
      rel="stylesheet"
      type="text/css"
    />
  </head>
  <body class="body--dark">
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>
    <div id="q-app">
      <q-layout
        view="lHh Lpr lFf"
        container
        style="height: 100vh;"
        class="shadow-2 rounded-borders"
      >
        <div class="q-pa-md bg-grey-10 text-white">
          <q-btn-toggle
            v-model="model"
            push
            rounded
            glossy
            toggle-color="purple"
            :options="options"
          >
            <template v-slot:one>
              <div class="row items-center no-wrap">
                <div class="text-center">Pick<br />boat</div>
                <q-icon right name="directions_boat" />
              </div>
            </template>

            <template v-slot:two>
              <div class="row items-center no-wrap">
                <div class="text-center">Pick<br />car</div>
                <q-icon right name="directions_car" />
              </div>
            </template>

            <template v-slot:three>
              <div class="row items-center no-wrap">
                <div class="text-center">Pick<br />railway</div>
                <q-icon right name="directions_railway" />
              </div>
            </template>
          </q-btn-toggle>
        </div>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          model: "",
          options: [
            { value: "one", slot: "one" },
            { value: "two", slot: "two" },
            { value: "three", slot: "three" }
          ]
        }
      });
    </script>
  </body>
</html>

We populate the one , two , and three slot to populate the content of the buttons in the button toggle.

Also, we can disable the button toggle with the disabled prop:

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"
      rel="stylesheet"
      type="text/css"
    />
    <link
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"
      rel="stylesheet"
      type="text/css"
    />
  </head>
  <body class="body--dark">
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>
    <div id="q-app">
      <q-layout
        view="lHh Lpr lFf"
        container
        style="height: 100vh;"
        class="shadow-2 rounded-borders"
      >
        <div class="q-pa-md">
          <q-btn-toggle
            disable
            v-model="model"
            toggle-color="primary"
            push
            glossy
            :options="[
              {label: 'One', value: 'one'},
              {label: 'Two', value: 'two'},
              {label: 'Three', value: 'three'}
            ]"
          >
          </q-btn-toggle>
        </div>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          model: ""
        }
      });
    </script>
  </body>
</html>

We can set the readonly prop to disable the button group without changing it styles.

Conclusion

We can set various options with Quasar’s button group.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *